<?php
namespace App\Http\Controllers\Api\V2\QrCode;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\Common\Common;
use App\Http\Controllers\Business\App\Statistic\StatisticManagement;
use Log;


class QrCodeController extends ApiController
{
    //通过该商家id获取商家信息
    public function getShopByShopId() {
        $shopId = $this->request->get('shopid');
        $result = PublicBusiness::getBriefShopInfoByShopId($shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //用户扫描商家店里的二维码买单
    public function payByShopQrCode() {
        $userId = $this->userId;
        $shopId = $this->request->get('shopid');
        $money = $this->request->get('money');
        if(!$this->request->has('apiversion')) {
            //老版本
            if(date('Y-m-d')<'2017-03-01') {
                $result = PublicBusiness::collectMoney($userId, $shopId, $money);
            } else {
                return $this->responseError('请下载最新app');
            }
        } else {
            if($this->request->get('apiversion') == 'v2') {
                //v2版本
                $payCode = '';
                $shopUserId = '';
                $type = '';
                $result = Common::collectMoney($shopId,$money,'user', $userId, $payCode, $shopUserId, $type);
                if($this->request->has('iOSorAndroid')) {
                    $userAgent = $this->request->get('iOSorAndroid');
                } else {
                    $userAgent = $this->request->header('user-agent');
                }
                $ip = $this->request->ip();
                StatisticManagement::addUserLoginAfterInfo($userId, 'order', $userAgent, $ip);
            } else {
                return $this->responseError('版本不支持');
            }
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }

}